﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace DrawIO45.Models.Logic
{
    /// <summary>
    /// 类型信息
    /// </summary>
    public class LClassInfo
    {
        /// <summary>
        /// 类名
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// id
        /// </summary>
        public string Id { get; set; } = Guid.NewGuid().ToString();

        /// <summary>
        /// 类类型
        /// </summary>
        public LClassType ClassType { get; set; }

        ///// <summary>
        ///// 备注
        ///// </summary>
        //public string Remarks { get; set; }

        ///// <summary>
        ///// 函数Id
        ///// </summary>
        //public string Id { get; set; }

        /// <summary>
        /// 全名
        /// </summary>
        public string FullName { get; set; }

        /// <summary>
        /// 父级对象
        /// </summary>
        public LClassInfo Parent { get; set; }

        /// <summary>
        /// 类型来源
        /// </summary>
        public Type Source { get; set; }

        ///// <summary>
        ///// 依赖的内容
        ///// </summary>
        //public List<LCalssInfo> Dependents { get; set; }

        public LClassInfo()
        {
            _MessageInfos = new Lazy<List<LMethodInfo>>(() =>
            {
                if (Source == null)
                    return null;

                var methods = Source.GetMethods();

                if (methods?.Any() != true)
                    return null;

                var igNameHead = new[]
                {
                    "get_",
                    "set_",
                };

                return methods.Where(n => igNameHead.All(i => n.Name.StartsWith(i) != true))
                    .Where(n => n.DeclaringType == Source)
                    .Select(n => LMethodInfo.GetLMethodInfo(n))
                    .ToList();
            });

            _Properties = new Lazy<List<LPropertyInfo>>(() =>
            {
                if (Source == null)
                    return null;

                var properties = Source.GetProperties();
                if (properties?.Any() != true)
                    return null;

                return properties.Select(n => new LPropertyInfo(n)).ToList();
            });
        }

        public string GetFormatName()
        {
            return Name;
        }

        Lazy<List<LMethodInfo>> _MessageInfos = null;

        /// <summary>
        /// 函数信息
        /// </summary>
        public List<LMethodInfo> MethodInfos => _MessageInfos.Value;

        Lazy<List<LPropertyInfo>> _Properties = null;

        public List<LPropertyInfo> Properties => _Properties.Value;

        public static LClassInfo GetClassInfo(Type type)
        {
            if (type == null)
                return null;

            var classInfo = new LClassInfo
            {
                ClassType = LClassType.Class,
                FullName = type.FullName,
                Name = type.Name,
                Parent = GetClassInfo(type.BaseType),
                Source = type,
            };

            return classInfo;
        }
    }

    public enum LClassType
    {
        Unknown,

        Void,

        Class,
    }
}
